﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BehaviorSystem
{
    /// <summary>
    /// 顺序节点
    /// </summary>
    public class SequenceNode : CompositeNode
    {

        public SequenceNode(IBehaviorAgent agent) : base(agent)
        {
            CurState = BTNodeState.Running;
        }

        public override BTNodeState HandleComposite()
        {
            if (curIndex < ChildrenNodes.Count)
            {
                ChildrenNodes[curIndex].ExcuteNode();


                if (ChildrenNodes[curIndex].CurState == BTNodeState.Success)
                {
                    curIndex++;
                    CurState = BTNodeState.Running;
                    if (curIndex >= ChildrenNodes.Count)
                    {
                        CurState = BTNodeState.Success;
                    }
                }
                else if (ChildrenNodes[curIndex].CurState == BTNodeState.Failure)
                {
                    curIndex = ChildrenNodes.Count;
                    CurState = BTNodeState.Failure;
                }
                else if (ChildrenNodes[curIndex].CurState == BTNodeState.Running)
                {
                    CurState = BTNodeState.Running;
                }
                else
                {
                    CurState = BTNodeState.Success;
                }
            }
            return CurState;
        }


    }
}